package objectClasses;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.SecureRandom;


public class Entry {
	private String Materials;
	private Image weaving_draft;
	private String idnumber;
	private User the_contestant;
	
	public Entry(String Con_Materials, Image their_draft,String filePath){
		Materials = Con_Materials;
		weaving_draft = their_draft;
		idnumber = assignID();
		writeToFile(Con_Materials,filePath);
	}
	public Entry(String Con_Materials, Image their_draft,String filePath, String ID){
		Materials = Con_Materials;
		weaving_draft = their_draft;		
		idnumber = ID;
	}

	public User getContestant(){
		return the_contestant;
	}
	  
	private String assignID(){
		SecureRandom random = new SecureRandom();
		return new BigInteger(20, random).toString(32);
	}
	
	public String getIdNumber(){
		return idnumber;
	}
	
		
	public Image getWeavingDraftImage(){
		return weaving_draft;
	}
	
	public String getEntryMaterials(){
		return Materials;
	}
	
	public void writeToFile(String Their_Materials,String ImageFilePath){
		
		try{
			
			BufferedReader in = new BufferedReader( new FileReader( "out.txt" ) );
			String line;  
	        StringBuilder buf = new StringBuilder();  
	        while( (line = in.readLine()) != null )  
	        {  
	            buf.append( line );  
	            buf.append("\r\n");
	            
	        }  
	        
	        in.close();  
	          
	        // modify file contents  
	   
	        
			  FileWriter fstream = new FileWriter("out.txt");
			  BufferedWriter out = new BufferedWriter(fstream);
			  out.write(buf.toString());
			  out.write("Their Materials: ");
			  out.write(Their_Materials);
			  out.write("\r\n");
			  out.write("File Path: ");
			  
			  out.write(ImageFilePath);
			  //Close the output stream
			  out.close();
			  }catch (Exception e){//Catch exception if any
			  System.err.println("File did not exist, created it");
			  File f = new File("out.txt");
			
				  try {
					f.createNewFile();
					
					writeToFile(Their_Materials,ImageFilePath);
				} catch (IOException e1) {
					System.out.print("Could not created a file?");
				}
			  }
		
	}
	
	
		
	


}
